<?php
    

    $address = 'localhost';
    $port    = 10001;
	if (FALSE == ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
		die("Couldn't work with socket, error code is: " . socket_last_error() .",error message is: " . socket_strerror(socket_last_error()));
    }
    if (FALSE == ($ret = socket_bind($sock, $address, $port))) {
        die("Couldn't work with socket, error code is: " . socket_last_error() .",error message is: " . socket_strerror(socket_last_error()));
    }
	if (FALSE == ($ret = socket_listen($sock, 1))) {
		die("Couldn't work with socket, error code is: " . socket_last_error() .",error message is: " . socket_strerror(socket_last_error()));
    }
	do {
		if (FALSE == ($msgsock = socket_accept($sock))) {
			die("Couldn't work with socket, error code is: " . socket_last_error() .",error message is: " . socket_strerror(socket_last_error()));
		}
		else{
			$t = true;
			do {
				if (FALSE == ($buf = socket_read($msgsock, 1024))){
					die ('socket_read() failed: '.socket_strerror(socket_last_error()));
				}
				if ($buf == "close socket"){
					socket_write($msgsock, $buf);
					socket_close($msgsock);
					$t = false;
				}
				else{
					socket_write($msgsock, $buf);
				}
			} while ($t);
		}
	} while (true);
?>